package com.senriot.quickup.web.rest

import com.senriot.quickup.service.MaService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import me.chanjar.weixin.mp.api.WxMpService
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.math.BigDecimal

@RestController
@RequestMapping("api/ma")
@Api(tags = ["小程序接口"])
class MaResource(private val wxMpService: WxMpService,
                 private val maService: MaService)
{

    @GetMapping("login")
    @ApiOperation("登录")
    fun login(code: String): Map<String, Any?>
    {
        return maService.login(code)
    }

    @GetMapping("token")
    fun getToken(sessionKey: String, encryptedData: String, ivStr: String): String?
    {
        return maService.getToken(sessionKey, encryptedData, ivStr)
    }

    @GetMapping("news")
    @ApiOperation("获取文章")
    fun getNews(): WxMpMaterialNewsBatchGetResult?
    {
        return wxMpService.materialService.materialNewsBatchGet(0, 20)
    }

    @GetMapping("statistics")
    @ApiOperation("获取统计数据")
    fun getStatistics(unionId: String) = maService.getUserStatistics(unionId)

    @GetMapping("my-devices")
    @ApiOperation("获取设备列表")
    fun getMyDevices() = maService.getMyDevices()

    @GetMapping("bonus")
    @ApiOperation("获取分成信息")
    fun getBonus() = maService.getBonus()

    @GetMapping("orders")
    @ApiOperation("获取订单信息")
    fun getOrders(unionId: String) = maService.getOrders(unionId)

    @GetMapping("transfer")
    @ApiOperation("转账到零钱")
    fun transfer(openId: String, amount: BigDecimal) = maService.transfer(openId, amount)
}
